param(
    [string]$RemoteName = "origin",
    [string]$SourceBranch = "master",
    [string]$TargetBranch = "upm",
    [string]$TagName = "1.0.0",
    [string]$PackagePath = "Assets/com.bear.package.singleton",
    [string]$CommitMessage = "Release $TagName"
)

Write-Host "开始创建 UPM 分支和标签..." -ForegroundColor Green
Write-Host "远程仓库: $RemoteName"
Write-Host "源分支: $SourceBranch"
Write-Host "目标分支: $TargetBranch"
Write-Host "标签名称: $TagName"
Write-Host "包路径: $PackagePath"

# 使用 cmd /c 来执行 Git 命令，避免 PowerShell 解析问题
function Execute-GitCommand {
    param([string]$Command)
    
    Write-Host "执行: git $Command" -ForegroundColor Cyan
    $result = cmd /c "git $Command" 2>&1
    $exitCode = $LASTEXITCODE
    
    if ($exitCode -ne 0) {
        Write-Warning "Git 命令失败 (退出码: $exitCode): $Command"
        Write-Warning "错误信息: $result"
    } else {
        Write-Host "命令执行成功" -ForegroundColor Green
    }
    
    return $exitCode
}

try {
    # 1. 切换到源分支
    Write-Host "切换到源分支 $SourceBranch..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "checkout $SourceBranch"
    if ($exitCode -ne 0) { throw "无法切换到源分支 $SourceBranch" }

    # 2. 拉取最新的源分支
    Write-Host "拉取最新的源分支..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "pull $RemoteName $SourceBranch"
    if ($exitCode -ne 0) { throw "无法拉取源分支" }

    # 3. 检查包路径是否存在
    Write-Host "检查包路径 $PackagePath 是否存在..." -ForegroundColor Yellow
    if (-not (Test-Path $PackagePath)) {
        throw "包路径 $PackagePath 不存在"
    }
    Write-Host "包路径验证通过" -ForegroundColor Green

    # 4. 删除本地目标分支（如果存在）
    Write-Host "检查并删除本地目标分支 $TargetBranch..." -ForegroundColor Yellow
    $branchesOutput = cmd /c "git branch" 2>&1
    if ($branchesOutput -match "\s+$TargetBranch\s*$") {
        $exitCode = Execute-GitCommand "branch -D $TargetBranch"
        if ($exitCode -ne 0) { Write-Warning "无法删除本地分支 $TargetBranch" }
    } else {
        Write-Host "本地分支 $TargetBranch 不存在，跳过删除"
    }

    # 5. 使用 git subtree split 创建 UPM 分支
    Write-Host "使用 git subtree split 创建 UPM 分支..." -ForegroundColor Yellow
    Write-Host "将 $PackagePath 目录分离到 $TargetBranch 分支" -ForegroundColor Cyan
    $exitCode = Execute-GitCommand "subtree split --prefix=$PackagePath --branch $TargetBranch"
    if ($exitCode -ne 0) { throw "无法创建 UPM 分支" }

    # 6. 切换到目标分支
    Write-Host "切换到目标分支 $TargetBranch..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "checkout $TargetBranch"
    if ($exitCode -ne 0) { throw "无法切换到目标分支 $TargetBranch" }

    # 7. 删除远程的旧标签（如果存在）
    Write-Host "删除远程旧标签 $TagName..." -ForegroundColor Yellow
    $remoteTagsOutput = cmd /c "git ls-remote --tags $RemoteName" 2>&1
    if ($remoteTagsOutput -match "refs/tags/$TagName") {
        $exitCode = Execute-GitCommand "push $RemoteName :refs/tags/$TagName"
        if ($exitCode -ne 0) { Write-Warning "无法删除远程标签" }
    } else {
        Write-Host "远程标签 $TagName 不存在，跳过删除"
    }

    # 8. 删除本地标签（如果存在）
    Write-Host "删除本地标签 $TagName..." -ForegroundColor Yellow
    $localTagsOutput = cmd /c "git tag -l" 2>&1
    if ($localTagsOutput -contains $TagName) {
        $exitCode = Execute-GitCommand "tag -d $TagName"
        if ($exitCode -ne 0) { Write-Warning "无法删除本地标签" }
    } else {
        Write-Host "本地标签 $TagName 不存在，跳过删除"
    }

    # 9. 在目标分支创建标签
    Write-Host "在目标分支创建标签 $TagName..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "tag -a $TagName -m `"$CommitMessage`""
    if ($exitCode -ne 0) { throw "无法创建标签" }

    # 10. 推送目标分支和标签到远程
    Write-Host "推送目标分支和标签到远程..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "push $RemoteName $TargetBranch --tags"
    if ($exitCode -ne 0) { throw "无法推送分支和标签" }

    # 11. 切换回源分支
    Write-Host "切换回源分支 $SourceBranch..." -ForegroundColor Yellow
    $exitCode = Execute-GitCommand "checkout $SourceBranch"
    if ($exitCode -ne 0) { Write-Warning "无法切换回源分支" }

    Write-Host "操作完成！" -ForegroundColor Green
    Write-Host "UPM 分支 $TargetBranch 已创建并推送到远程" -ForegroundColor Green
    Write-Host "标签 $TagName 已创建并推送到远程" -ForegroundColor Green
    Write-Host "包路径 $PackagePath 已分离到独立分支" -ForegroundColor Green

    # 显示当前状态
    Write-Host "当前状态:" -ForegroundColor Yellow
    cmd /c "git branch -a"
    cmd /c "git tag -l | findstr $TagName"

} catch {
    Write-Error "操作失败: $($_.Exception.Message)"
    exit 1
}